﻿<%@ WebHandler Language="C#" Class="Login" %>

using System;
using System.Web;

using EHSubmit.Logic;
using EHSubmit.Logic.Services;
using EHSubmit.Model;
using EHSubmit.Util;

public class Login : BaseHandler
{

    public override void ProcessRequest(HttpContext context)
    {
        User user = new User();
        user.ID = context.Request["uid"] as string;
        user.Pwd = context.Request["pwd"] as string;
        user.RoleId = WebHelper.IntSafeConvert(context.Request["role"], Constants.ROLE_STUDENT);
        
        // 用户名密码不能为空
        if (user.ID == null || user.Pwd == null)
        {
            return;
        }

        string responseText = null;    // 响应客户的数据
        
        // 验证
        UserLoginProvider ulp = new UserLoginProvider();

        try
        {
            User checkedUser = ulp.Check(user);

            //--- 根据检验结果响应客户


            if (checkedUser != null)
            {
                if (checkedUser.RoleId != user.RoleId) // 角色选择错误
                {
                    responseText = "{success:false,msg:'您不是该类用户'}";
                }
                else
                {
                    
                    // 将用户信息存入Session中
                    context.Session[Constants.SESS_USER_KEY] = checkedUser;
                    checkedUser.LastLoginId = context.Request.UserHostAddress;
                    UserManager mgr = new UserManager();
                    mgr.DoLoginSuccessed(checkedUser);
                    
                    // 根据不同角色返回将跳转的页面

                    switch (checkedUser.RoleId)
                    {
                        case 0:  // admin
                            responseText = "{success:true,msg:'Admin'}";
                            break;
                        case 1:  // teacher
                            responseText = "{success:true,msg:'Teacher'}";
                            break;
                        case 2:  // student
                            responseText = "{success:true,msg:'Student'}";
                            break;

                    }

                }
            }
            else
            {
                responseText = "{success:false,msg:'用户名或密码错误!'}";
            }
        }
        catch (SysException se)
        {
            responseText = "{success:false,msg:'" + se.Message + "'}";
        }

        context.Response.Write(responseText);
    }

}